from sqlalchemy import Column, String, Integer, ForeignKey, DateTime, Text, Boolean
from sqlalchemy.orm import relationship

from app.models.base import Base


class Document(Base):
    """文档模型"""
    title = Column(String(100), nullable=False, index=True)
    description = Column(Text, nullable=True)
    file_path = Column(String(255), nullable=False)  # 文件路径
    document_type = Column(String(50), nullable=False)  # 文档类型
    mime_type = Column(String(100), nullable=False)  # MIME类型
    file_size = Column(Integer, nullable=False)  # 文件大小（字节）
    version = Column(String(20), nullable=False, default="1.0")  # 版本号
    is_latest = Column(Boolean, default=True)  # 是否最新版本
    
    # 外键
    project_id = Column(Integer, ForeignKey("project.id"), nullable=False)
    creator_id = Column(Integer, ForeignKey("user.id"), nullable=False)
    task_id = Column(Integer, ForeignKey("task.id"), nullable=True)  # 关联任务
    parent_id = Column(Integer, ForeignKey("document.id"), nullable=True)  # 上一版本文档ID
    
    # 关系
    project = relationship("Project", back_populates="documents")
    creator = relationship("User")
    task = relationship("Task", back_populates="documents")
    previous_version = relationship("Document", remote_side=[id], backref="next_version", uselist=False)